package cn.stylefeng.guns.service.impl;

import cn.stylefeng.guns.modular.demo.service.SyncService;
import cn.stylefeng.roses.kernel.timer.api.TimerAction;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import lombok.extern.slf4j.Slf4j;
import org.jboss.logging.MDC;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * 数据同步任务
 */
@Component
@Slf4j
public class YearMailTimer implements TimerAction {


    @Resource
    private SyncService syncService;
    public static DateTimeFormatter SIMPLE_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyyMMdd");


    @Override
    public void action(String params) {
        String traceId = UUID.randomUUID().toString();
        MDC.put("traceId", traceId);
        int day = 2;
        log.info("同步门店数据");
        syncService.syncStoreData();
        log.info("同步供应商 商品数据");
        syncService.syncData();
        log.info("同步进销存数据");
        //同步前天的数据
        LocalDateTime localDateTime = LocalDateTime.now().minusDays(day);
        String format = SIMPLE_DATE_FORMAT.format(localDateTime);
        syncService.syncMaterialData(format);
        try {
            log.info("generateTestData begin");
            syncService.syncTestData(format);
        } catch (Exception e) {
            log.info("同步测试数据失败", e);
        }

        log.info("计算库存");
        syncService.calStock(day);
    }

    public static void main(String[] args) {
        //同步前天的数据
        LocalDateTime localDateTime = LocalDateTime.now().minusDays(1);
        System.out.println(SIMPLE_DATE_FORMAT.format(localDateTime));
    }
}
